const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    VCNum: 60,   //验证码时间
    mobile: null,  //手机号
    CodeVal: null,    //验证码
  },
  onLoad(options){
    this.setData({
      mobile: options.mobile
    });
    this.VCcodeTime();
  },
  
  /*分享*/ 
  onShareAppMessage(){
    
  },
  

  /*验证码*/
  Code(e) {
    let CodeVal = e.detail.value;
    this.setData({
      CodeVal
    });
  },

  /**
   * @explain form 提交手机号和短信验证 
   * @param phone 手机号
   * @param code 短信
   * */ 
  formSubmit(e){
    let td = this.data, code = e.detail.value.code;
    let RegInfo = {
      mobile: td.mobile,
      code
    };
    if (code.length == 6){
      wx.showLoading({
        title: "短信效验中...",
        mask: true,
      });
      SERVICE.POST(SERVICE.URL.verifyPhone, {
        phone: td.mobile,
        code: code
      }, (status, res) => {
        console.log("提交手机号和短信验证:",res);
        wx.hideLoading();
        if (status) {
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true,
            duration: 1500,
            success: function () {
              let RegInfo = {
                mobile: td.mobile,
                code
              };
              setTimeout(() => {
                wx.navigateTo({
                  url: "../pwd-mobile/pwd-mobile?RegInfo=" + JSON.stringify(RegInfo),
                })
              }, 1500);
            }
          })

        } else {
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true,
            duration: 1500
          })
        }
       
      });
    }
   
   
  },
  
  /**
   * @explain getVc 获取验证码
   * @param phone 手机号
   * */ 
  getCode(){
    let _this = this, td = this.data;  
    if (td.VCNum == 60){
      SERVICE.POST(SERVICE.URL.vccode, {
        phone: td.mobile,
        register: "register",
      }, (status, res) => {
        if (status) {
          _this.VCcodeTime();          
        }
      });
    }
  },

  /*验证码时间*/
  VCcodeTime(){
    let _this = this, td = this.data;    
    if (td.VCNum >= 60) {
      let inter = setInterval(callback => {
        _this.setData({
          VCNum: --td.VCNum
        });
        if (td.VCNum == 0) {
          td.VCNum = 60;
          clearInterval(inter);
        }
      }, 1000);
    }
  }
});